<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/10  10:18
 */
namespace app\admin\controller;

class AdminMember extends Common
{
    private $table_admin;
    /*
	*	构造函数
	*/
    function __construct(){
        parent::__construct();
        $this->table_admin = db('admin_member');
    }

    //会员列表
    public function lists(){
        $pageParam = ['query' =>[]];
        $where = [];

        $keyword = input("keyword");
        if($keyword != ''){
            $where['account']=array("like",'%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
        }

        $list = $this->table_admin->where($where)->order('id desc')->paginate(15, false, $pageParam);
        $page = $list->render();
        $this->assign('page',$page);
        $this->assign('users',$list);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }


    //详情
    public function detail()
    {
        $id  = input('id');
        $data = $this->table_admin->where(['id'=>$id])->find();

        if(empty($data))return $this->error('数据不存在');
        $this->assign('da', $data);
        return $this->fetch();
    }

    //添加账户
    public function add()
    {
        if(request()->isAjax()){
            $id = input('id');
            $data['account'] = input('account');
            $data['password'] = md5(input('password'));
            $data['balance'] = input('balance');
            $data['status'] = input('status');


            if(!empty($data['balance'])){
                $insert_data['user_id'] = $id;
                $insert_data['type'] = 1;
                $insert_data['price'] = $data['balance'];
                $insert_data['createtime'] = time();
                db('amount_log')->insert($insert_data);
            }

            if(!empty($id)){
                $account = $this->table_admin->where(['id'=>$id])->find();

                //修改
                if(!empty($data['balance'])){
                    $res = $this->table_admin->where(['id'=>$id])->setInc('balance',$data['balance']);
                }
                
                if($account['status'] != $data['status']){
                    $update_data['status'] = $data['status'];
                    $res = $this->table_admin->where(['id'=>$id])->update($update_data);
                }
            }else{
                $account = $this->table_admin->where(['account'=>$data['account']])->find();

                if(!empty($account)){
                    $this->error('账号名重复！');
                }

                //添加
                $data['ip'] = get_client_ip();
                $data['creatime']   = time();
                $res = $this->table_admin->insert($data);
            }
            if($res)$this->success('操作成功！', url('AdminMember/lists'));
            $this->error('操作失败！');
        }
        return $this->fetch();
    }


    //充值列表
    public function certificate()
    {
        $keyword = input('keyword');
        $check = input('check');
        $where = [];
        if(!empty($keyword)){
            $where['am.account'] = ['like',"%$keyword%"];
        }

        if(isset($check)){
            $where['c.check'] = $check;
        }

        $list = db('certificate')->alias('c')->join('admin_member am','am.id=c.uid')->where($where)->order('is_reading asc')->field('c.is_reading,c.id,c.image,c.check,c.creatime,am.account')->paginate(15)->each(function($item, $key){
            $item['image'] = IMG_PATH.$item['image'];
            return $item;
        });

        $page = $list->render();
        $this->assign('page',$page);
        $this->assign('users', $list);
        $this->assign('keyword', $keyword);
        $this->assign('check', $check);
        return $this->fetch();
    }

    //充值详情状态更改
    public function certificate_detail()
    {
        $id  = input('id');
        $data = db('certificate')->where(['id'=>$id])->find();
        $data['image'] = IMG_PATH.$data['image'];
        $account = db('admin_member')->where(['id'=>$data['uid']])->value('account');

        //修改已查看状态
        $update_certificate['is_reading'] = 1;
        db('certificate')->where(['id'=>$id])->update($update_certificate);

        if(empty($data))return $this->error('数据不存在');
        $this->assign('da', $data);
        $this->assign('account', $account);
        return $this->fetch();
    }

    //更改用户
    public function certificate_detail_do()
    {
        if(request()->isAjax()){
            $data['balance'] = input('balance');
            $uid = input('uid');
            $id = input('id');
            $status = input('status');

            if(!empty($data['balance'])){
                $insert_data['user_id'] = $uid;
                $insert_data['type'] = 1;
                $insert_data['price'] = $data['balance'];
                $insert_data['createtime'] = time();
                db('amount_log')->insert($insert_data);
            }

            //修改
            if(!empty($data['balance'])){
                $this->table_admin->where(['id'=>$uid])->setInc('balance',$data['balance']);
            }

            $update_certificate['check'] = $status;
            $res = db('certificate')->where(['id'=>$id])->update($update_certificate);
            if(!$res)$this->error('修改失败!');
            $this->success('修改成功!',url('AdminMember/certificate'));
        }
    }
}